<template>
	<view class="w-tit-back" :style="{background: background }">
		<view :class="{ 'w-tit-back--fixed': fixed, 'w-tit-back--shadow': border, 'w-tit-back--border': border }"
			:style="{ 'background': background}" class="w-tit-back__content">
			<uni-status-bar v-if="statusBar" />
			<view :style="{ color: color}" class="w-tit-back__header w-tit-back__content_view">
				<template v-if="!is_search">
					<view @tap="onClickLeft"
						class="w-tit-back__header-btns w-tit-back__header-btns-left w-tit-back__content_view">
						<view class="w-tit-back__content_view" v-if="leftIcon.length">
							<uni-icons :color="color" :type="leftIcon" :size="iconfont" />
						</view>
						<view :class="{ 'w-tit-back-btn-icon-left': !leftIcon.length }"
							class="w-tit-back-btn-text w-tit-back__content_view" v-if="leftText.length">
							<text :style="{ color: color, 'font-size': '14px' }">{{ leftText }}</text>
						</view>
						<slot name="left" />
					</view>
					<view class="w-tit-back__header-container w-tit-back__content_view">
						<view class="w-tit-back__header-container-inner w-tit-back__content_view" v-if="title.length">
							<text class="uni-nav-bar-text"
								:style="{'color': titcolor , 'font-size':fontSize +'px','font-weight':fontweight }">{{ title }}</text>
						</view>
						<!-- 标题插槽 -->
						<slot />
					</view>
					<template v-if="rightIconNum==='one'">
						<view :class="title.length ? 'w-tit-back__header-btns-right' : ''" @tap="onClickRight"
							class="w-tit-back__header-btns w-tit-back__content_view">
							<view class="w-tit-back__content_view" v-if="rightIcon.length">
								<uni-icons :color="color" :type="rightIcon" :size="iconfont" fontWeight="600" />
							</view>
							<!-- 优先显示图标 -->
							<view class="w-tit-back-btn-text w-tit-back__content_view"
								v-if="rightText.length && !rightIcon.length">
								<text class="uni-nav-bar-right-text"
									:style="{color:rightColor,'font-size':rightSize + 'px'}">{{ rightText }}</text>
							</view>
							<slot name="right" />
						</view>
					</template>
					<template v-else-if="rightIconNum==='two'">
						<view :class="title.length ? 'w-tit-back__header-btns-right' : ''"
							class="w-tit-back__header-btns w-tit-back__content_view">
							<image class="icon-tb" :src="$mAssetsPath.icon_search" @click="rightSearch(is_search)">
							</image>
							<image class="icon-tb2" :src="$mAssetsPath.icon_add" @click="rightAdd"></image>
							<!-- 优先显示图标 -->
							<view class="w-tit-back-btn-text w-tit-back__content_view"
								v-if="rightText.length && !rightIcon.length">
								<text class="uni-nav-bar-right-text"
									:style="{color:rightColor,'font-size':rightSize + 'px'}">{{ rightText }}</text>
							</view>
							<slot name="right" />
						</view>
					</template>
				</template>
				<template v-else-if="is_search">
					<view @tap="onClickLeft2(is_search)"
						class="w-tit-back__header-btns2 w-tit-back__header-btns-left2 w-tit-back__content_view">
						<view class="w-tit-back__content_view" v-if="leftIcon.length">
							<uni-icons :color="color" :type="title===''?'arrowleft':'closeempty'" size="28" />
						</view>
					</view>
					<view class="w-tit-back__header-container w-tit-back__content_view">
						<view class="w-tit-back__header-container-inner w-tit-back__content_view">
							<view class="input-row">
								<image class="icon-tb3" :src="$mAssetsPath.icon_search2"></image>
								<input class="input-row-w" type="text" :focus="showSync" v-model="searchVal" placeholder="搜索" confirm-type="search"
								 placeholder-style="color:#ddd" @confirm="confirm"></input> <!-- @click.stop="confirm" -->
								<image class="icon-tb3" v-if="searchVal!=''" @click.stop="clear" :src="$mAssetsPath.icon_qingchu"></image>
							</view>
						</view>
						<!-- 标题插槽 -->
						<slot />
					</view>
					<view :class="title.length ? 'w-tit-back__header-btns-right2' : ''" @tap="confirm"
						class="w-tit-back__header-btns w-tit-back__content_view">
						<!-- 优先显示图标 -->
						<view class="w-tit-back-btn-text w-tit-back__content_view">
							<text class="uni-nav-bar-right-text2"
								:style="{color:rightColor2,'font-size':rightSize2 + 'px'}">搜索</text>
						</view>
						<slot name="right" />
					</view>
				</template>
			</view>
		</view>
		<view class="w-tit-back__placeholder" v-if="fixed">
			<uni-status-bar v-if="statusBar" />
			<view class="w-tit-back__placeholder-view" />
		</view>
	</view>
</template>

<script>
	import uniStatusBar from "@/components/uni-status-bar/uni-status-bar.vue";
	import uniIcons from "@/components/uni-icons/uni-icons.vue";

	export default {
		name: "w-tit-back",
		components: {
			uniStatusBar,
			uniIcons
		},
		props: {
			rightIconNum: {
				type: String,
				default: "one"
			},
			title: {
				type: String,
				default: ""
			},
			leftText: {
				type: String,
				default: ""
			},
			rightText: {
				type: String,
				default: ""
			},
			leftIcon: {
				type: String,
				default: ""
			},
			iconfont: {
				type: String,
				default: "28"
			},
			rightIcon: {
				type: String,
				default: ""
			},
			rightColor: {
				type: String,
				default: ""
			},
			rightSize: {
				type: String,
				default: ""
			},
			rightColor2: {
				type: String,
				default: "#198FFF"
			},
			rightSize2: {
				type: String,
				default: "36rpx"
			},
			fixed: {
				type: [Boolean, String],
				default: false
			},
			color: {
				type: String,
				default: "#fff"
			},
			titcolor: {
				type: String,
				default: "#333333"
			},
			fontweight: {
				type: String,
				default: "300"
			},
			fontSize: {
				type: String,
				default: "16"
			},
			background: {
				type: String,
				default: "rgba(255,255,255,0)"
			},
			statusBar: {
				type: [Boolean, String],
				default: false
			},
			shadow: {
				type: [String, Boolean],
				default: true
			},
			border: {
				type: [String, Boolean],
				default: true
			},
			is_back: {
				type: [String, Boolean],
				default: true
			},
			is_searchs:{
				type: [String, Boolean],
				default: false
			}
		},
		data() {
			return {
				is_search: false,
				searchVal:'',
				showSync: false,
			}
		},
		created() {
			this.is_search = this.is_searchs;
		},
		mounted() {
			if (uni.report && this.title !== '') {
				uni.report('title', this.title)
			}
		},
		methods: {
			clear() {
				this.searchVal = "";
				this.$nextTick(() => {
					this.showSync = true;
				})
			},
			confirm() {
				// #ifndef APP-PLUS
				uni.hideKeyboard();
				// #endif
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord()
				// #endif
				this.$emit("confirm", {
					value: this.searchVal
				})
			},
			rightSearch(is_search) {
				this.is_search = !is_search;
				this.$nextTick(() => {
					this.showSync = true;
				})
			},
			rightAdd() {
				this.$emit("rightAdd");
			},
			onClickLeft2(is_search) {
				if(this.title===''){
					uni.navigateBack({
						delta: 1
					})
				}else{
					this.is_search = !is_search;
					this.searchVal = "";
					this.$nextTick(() => {
						this.showSync = false;
					})
				}
				// #ifndef APP-PLUS
				uni.hideKeyboard()
				// #endif
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord()
				// #endif
				
				
			},
			onClickLeft() {
				if (this.is_back) {
					uni.navigateBack({
						delta: 1
					})
				} else {
					this.$emit("clickLeft");
				}

			},
			onClickRight() {
				this.$emit("clickRight");
			}
		}
	};
</script>

<style lang="scss" scoped>
	.input-row{
		display: flex;
		align-items: center;
		width: 100%;
		height: 66rpx;
		background: #fff;
		border-radius: 33rpx;
		background: $uni-bg-color-grey;
		.icon-tb3 {
			width: 34rpx;
			height: 34rpx;
			margin: 0 20rpx;
		}
		.icon-tb4 {
			width: 36rpx;
			height: 36rpx;
			margin-right: 20rpx;
		}
		.input-row-w{
			font-size: 28rpx;
			font-weight: 400;
			line-height: 20px;
			color: $font-color-9;
			width: calc(100% - 140rpx);
		}
	}
	.icon-tb {
		width: 34rpx;
		height: 34rpx;
	}

	.icon-tb2 {
		width: 34rpx;
		height: 34rpx;
		margin-left: 40rpx;
	}

	.uni-nav-bar-text {
		/* #ifdef APP-PLUS */
		font-size: 34rpx;
		/* #endif */
		/* #ifndef APP-PLUS */
		font-size: 32rpx;
		/* #endif */
	}

	.uni-nav-bar-right-text {
		font-size: 28rpx;
	}

	.w-tit-back {
		width: 750rpx;
		height: 90rpx;
		padding: 15rpx 6rpx;
	}

	.w-tit-back__content {
		position: relative;
		width: 750rpx;
		background-color: rgba(255, 255, 255, 0);
		overflow: hidden;
	}

	.w-tit-back__content_view {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		align-items: center;
		flex-direction: row;
		/* background-color: #FFFFFF;
 */
	}

	.w-tit-back__header {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		width: 750rpx;
		height: 60rpx;
		line-height: 60rpx;
		font-size: 32rpx;
	}

	.w-tit-back__header-btns {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-wrap: nowrap;
		width: 130rpx;
		justify-content: center;
		align-items: center;
	}

	.w-tit-back__header-btns-left {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		width: 130rpx;
		justify-content: flex-start;
	}
	.w-tit-back__header-btns-left2 {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		width: 70rpx;
		justify-content: flex-start;
	}
	.w-tit-back__header-btns2 {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-wrap: nowrap;
		width: 70rpx;
		justify-content: center;
		align-items: center;
	}

	.w-tit-back__header-btns-right {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		width: 150rpx;
		padding-right: 30rpx;
		justify-content: flex-end;
	}
	.w-tit-back__header-btns-right2 {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		width: 110rpx;
		padding-right: 30rpx;
		justify-content: flex-end;
	}

	.w-tit-back__header-container {
		flex: 1;
	}

	.w-tit-back__header-container-inner {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex: 1;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
	}


	.w-tit-back__placeholder-view {
		height: 30px;
	}

	.w-tit-back--fixed {
		position: fixed;
		z-index: 998;
	}

	.w-tit-back--shadow {}

	.w-tit-back--border {}
</style>
